package com.book.system.servlet;

import com.book.system.commom.R;
import com.book.system.entity.User;
import com.book.system.service.LendListService;
import com.book.system.service.impl.LendListServiceImpl;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author ：oath
 * @date ：Created in 2024/5/15 0:00
 * 借还管理控制层
 */
@Slf4j
@WebServlet(name = "LendServlet", urlPatterns = "/lendList")
public class LendListServlet extends BaseServlet {

    private static final LendListService lendListService = new LendListServiceImpl();


    /**
     * 获取所有借还数据
     * @author : oath
     * @create time: 2024/5/15 0:02
     * @param request 请求对象
     * @param response 响应对象
     * @return com.book.system.commom.R<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     */
    public R<List<Map<String, Object>>> getList(HttpServletRequest request, HttpServletResponse response) {

        // 获取前端传递的参数
        String flag = request.getParameter("flag");
        // 调用业务逻辑层方法
        List<Map<String, Object>> list = lendListService.getList(Integer.valueOf(flag));
        return R.success(list);

    }

    /**
     * 催还方法
     * @author : oath
     * @create time: 2024/5/15 22:05
     * @param request 请求对象
     * @param response 响应对象
     * @return com.book.system.commom.R<java.lang.String>
     */
    public R<String> updateById(HttpServletRequest request, HttpServletResponse response) {
        // 获取前端传递的参数
        String serNum = request.getParameter("ser_num");
        int i =lendListService.updateById(serNum);
        if (i == 1) {
            return R.success("催还成功");
        } else {
            return R.error("催还失败");
        }
    }

    /**
     * 根据借还编号删除数据
     * @author : oath
     * @create time: 2024/5/15 22:11
     * @param request 请求对象
     * @param response 响应对象
     * @return com.book.system.commom.R<java.lang.String>
     */
    public R<String> deleteById(HttpServletRequest request, HttpServletResponse response) {

        // 获取前端传递的参数
        String serNum = request.getParameter("ser_num");
        if (lendListService.deleteById(serNum) == 1) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    /**
     * 借阅图书
     *
     * @param request  请求对象
     * @param response 响应对象
     * @return com.book.system.commom.R<java.lang.String>
     * @author : oath
     * @create time: 2024/5/15 22:44
     */
    public R<String> borrowingBook(HttpServletRequest request, HttpServletResponse response) {

        //获取前端参数
        String bookId = request.getParameter("book_id");
        //获取当前登陆者的id
        User user = (User) request.getSession().getAttribute("user");
        log.info("当前登陆者id为：{}",user);

        //调用业务逻辑层方法
        int i = lendListService.borrowingBook(user.getId(),Integer.valueOf(bookId));
        if (i == 1) {
            return R.success("借书成功");
        } else {
            return R.error("借书失败");
        }
    }
}